package com.pray.praymall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pray.common.utils.PageUtils;
import com.pray.common.utils.Query;
import com.pray.praymall.member.dao.MemberDao;
import com.pray.praymall.member.entity.MemberEntity;
import com.pray.praymall.member.entity.MemberLevelEntity;
import com.pray.praymall.member.exception.PhoneExistsException;
import com.pray.praymall.member.exception.UserNameExistsException;
import com.pray.praymall.member.service.MemberLevelService;
import com.pray.praymall.member.service.MemberService;
import com.pray.praymall.member.vo.MemberLoginVo;
import com.pray.praymall.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(MemberRegisterVo vo) throws PhoneExistsException, UserNameExistsException {
        MemberEntity entity = new MemberEntity();
        // 设置默认等级
        MemberLevelEntity levelEntity = memberLevelService.getDefaultLevel();
        entity.setLevelId(levelEntity.getId());
        entity.setNickname(vo.getUserName());

        // 检查用户名和手机号是否唯一，为了让controller能感知到异常，异常机制
        checkPhoneUnique(vo.getPhone());
        checkUserNameUnique(vo.getUserName());

        entity.setMobile(vo.getPhone());
        entity.setUsername(vo.getUserName());

        // 密码要进行加密处理
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        entity.setPassword(encode);

        // 保存
        baseMapper.insert(entity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistsException {
        Integer result = baseMapper.checkPhoneUnique(phone);
        if (result != null) {
            throw new PhoneExistsException();
        }
    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameExistsException {
        Integer result = baseMapper.checkUserNameUnique(userName);
        if (result != null) {
            throw new UserNameExistsException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username", vo.getAccount()).or().eq("mobile", vo.getAccount()));

        if (entity != null) {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(vo.getPassword(), entity.getPassword());
            if (matches) {
                return entity;
            }
        }
        return null;
    }
}